package com.baox.agridata.synchronous.service.impl;

import com.baox.agridata.synchronous.dao.AsyncZSRETURNSInfoMapper;
import com.baox.agridata.synchronous.entity.ZsReturns;
import com.baox.agridata.synchronous.result.Result;
import com.baox.agridata.synchronous.service.AsyncZSRETURNSInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * packageName com.baox.agridata.synchronous.service.impl
 *
 * @author yanshuanglong
 * @version JDK 8
 * @className AsyncZSRETURNSInfoServiceImpl
 * @date 2025/2/26
 * @description TODO
 */
@Slf4j
@Service
public class AsyncZSRETURNSInfoServiceImpl implements AsyncZSRETURNSInfoService {
    @Autowired
    private AsyncZSRETURNSInfoMapper asyncZSRETURNSInfoMapper;


    @Override
    public Result dealReceiveListZSRETURNS(List<ZsReturns> lList) {
        long start = System.currentTimeMillis();
        try {
            if (lList != null && lList.size() > 0) {
                // 将 declareList 按批次拆分
                int batchSize = 100;
                List<List<ZsReturns>> subLists = new ArrayList<>();
                Map<String, Long> resultCount = new HashMap<>();
                resultCount.put("insert", 0L);
                resultCount.put("update", 0L);
                for (int i = 0; i < lList.size(); i += batchSize) {
                    subLists.add(lList.subList(i, Math.min(i + batchSize, lList.size())));
                }
                for (List<ZsReturns> subList : subLists) {
                    Map<String,Long> count = asyncZSRETURNSInfoMapper.insertIntoZSRETURNS(subList);

                    resultCount.put("insert", resultCount.get("insert") + count.getOrDefault("insert",0L));
                    resultCount.put("update", resultCount.get("update") + count.getOrDefault("update",0L));
                }
                log.info("本次同步畜禽标识数据总条数：{}，新增：{}，修改：{},耗时：{}",lList.size(),resultCount.get("insert"),resultCount.get("update"),System.currentTimeMillis() - start);
            }else {
                log.info("本次同步无畜禽标识无数据");
            }
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }


}
